//
//  AYMainTabController.swift
//  MyWeibo
//
//  Created by 昌俊 杨 on 29/9/2016.
//  Copyright © 2016 April. All rights reserved.
//

import UIKit

class AYMainTabController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        /** 添加子控制器 */
        setupChildControllers()
        setupComseBtn()
    }
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask
    {
        return .portrait
    }
    // @objc 允许这个函数在`运行时`通过 OC 的消息机制被调用！
    /** 撰写事件 */
    @objc func composeStates() -> () {
        print("+++++++++++")
    }
    lazy var composeBtn:UIButton =
        {
            let btn = UIButton.cz_imageButton("tabbar_compose_icon_add_highlighted", backgroundImageName: "tabbar_compose_button")
            return  btn!
    }()
}
// extension 类似于 OC 中的 分类，在 Swift 中还可以用来切分代码块
// 可以把相近功能的函数，放在一个 extension 中
// 便于代码维护
// 注意：和 OC 的分类一样，extension 中不能定义属性
// MARK: - 设置界面
extension AYMainTabController{
    
    func setupComseBtn() -> () {
        tabBar.addSubview(composeBtn)
        let count = CGFloat((viewControllers?.count)!)
        let width = tabBar.bounds.width/count - 1
        /** 在原来的rect基础上进行收缩，变形 insetBy */
        composeBtn.frame = tabBar.bounds.insetBy(dx: width*2, dy: 0)
        composeBtn.addTarget(self, action: #selector(composeStates), for: .touchUpInside)
    }
    //扩展的私有方法，主类访问不到
     func setupChildControllers()
    { 
        let array =
            [["clsName":"AYHomeViewController","title":"首页","imageName":"home"],["clsName":"AYMessageViewController","title":"消息","imageName":"message_center"],
             ["clsName":"UIViewController"],
        ["clsName":"AYDiscoverViewController","title":"发现","imageName":"discover"],
["clsName":"AYProfileViewController","title":"我","imageName":"profile"]]
        
        var arrayM = [UIViewController]()
        for dict in array
        {
            arrayM.append(controller(dict: dict))
        }
        /** tabBar的VCs */

        viewControllers = arrayM
        
    }
    /// 使用字典创建一个子控制器
    ///
    /// - parameter dict: 信息字典[clsName, title, imageName, visitorInfo]
    ///
    /// - returns: 子控制器

    private func controller(dict:[String: String])->UIViewController
    {
        guard let clsName = dict["clsName"],
        let title = dict["title"],
        let imageName = dict["imageName"],
        let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
            else
        {
            /** 此处返回的是该函数体需要的类型 */

            return UIViewController()
        }
        
        let vc = cls.init()
        vc.tabBarItem.image = UIImage(imageLiteralResourceName: "tabbar_"+imageName)
        vc.tabBarItem.selectedImage = UIImage(imageLiteralResourceName: "tabbar_"+imageName+"_selected").withRenderingMode(.alwaysOriginal)
        // 4. 设置 tabbar 的标题字体（大小）
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:#colorLiteral(red: 1, green: 0.5106488466, blue: 0.002952917945, alpha: 1)], for: .highlighted)
        // 系统默认是 12 号字，修改字体大小，要设置 Normal 的字体大小
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 14)], for: .normal)
        vc.title = title
        
        let nav = AYNavigationController(rootViewController: vc)
        
        return nav

    }
}
